﻿
var gotTaskTypes = false, gotPeople = false, gotUrgencyLevels = false, gotStates = false, gotProjects = false;
var taskId = null;

$(document).ready(function(){
    $("#LogoutButton").click(Logout);
    CheckIfLoggedIn();
    taskId = $.query.get('id');
    alert("Task id:" + taskId + "|");
    RequestAndSetChoices();
});

function RequestAndSetChoices(){
    LogosoftSharepoint.WebServices.EditTaskService.GetTaskTypes(SetAvailableTaskTypes);
    LogosoftSharepoint.WebServices.EditTaskService.GetPeople(SetAvailablePeople);
    LogosoftSharepoint.WebServices.EditTaskService.GetUrgencyLevels(SetAvailableUrgencyLevels);
    LogosoftSharepoint.WebServices.EditTaskService.GetStates(SetAvailableStates);
    LogosoftSharepoint.WebServices.EditTaskService.GetProjects(SetAvailableProjects);
}

function RequestTaskData(){
    if(gotTaskTypes == true 
            && gotPeople == true && gotUrgencyLevels == true 
            && gotStates == true && gotProjects == true){ // == true - inaczej undefined??
        if(taskId != null && taskId != ""){
            LogosoftSharepoint.WebServices.EditTaskService.GetTaskDetails(taskId, FillControlsWithTaskData);
        }else{
            var submitButton = $("<input>").attr("type", "button")
                                .attr("id", "SubmitDataButton")
                                .attr("value", "Dodaj")
                                .click(OnSubmitData);
                                
            submitButton.appendTo($("#SubmitChangesContainer"));
        }
    }
}

function OnSubmitData(event){
    // walidacja czy taskId jest liczba
    var numericExpression = /^[0-9]+$/;
    if(taskId != null && taskId != "" && !numericExpression.test(taskId)){
        return;
    }
    
    if(taskId == null || taskId == ""){
        var workers = new Array();
        $("#People :checked").each(function() {
                                        workers.push($(this).val());
                                    });
                                    
        var newTaskId = LogosoftSharepoint.WebServices.EditTaskService.InsertNewTask($("#ProjectSelect").val(),
                        $("#NameInput").val(), $("#DescribtionTextArea").val(),
                        $("#StatusSelect").val(), $("#TaskTypeSelect").val(),
                        $("#UrgencySelect").val(),
                        workers);
        if(newTaskId == -1){
            alert("Data could not be inserted");
            window.location(window.location);
        }else{
            alert("Data successfully inserted");
            $("#TaskID").html(newTaskId);
        }
    }else{
        var workers = new Array();
        $("#People :checked").each(function() {
                                        workers.push($(this).val());
                                    });
        
        var result = LogosoftSharepoint.WebServices.EditTaskService.ModifyTask(
                                taskId, $("#ProjectSelect").val(),
                                $("#NameInput").val(),
                                $("#DescribtionTextArea").val(), $("#StatusSelect").val(),
                                $("#TaskTypeSelect").val(), $("#UrgencySelect").val(),
                                workers);
        
        if(result == -1){ // error
            alert("Data could not be modified");
            window.location(window.location);
        }else{
            alert("Data successfully modified!");
        }
    }
    event.preventDefault();
}

function FillControlsWithTaskData(taskData){
    
    taskData = StripWebServiceResponse(taskData);
    
    // task id
    $("#TaskID").html(taskData.Task.id);
    
    // typ
    $("#TaskTypeSelect").val(taskData.Task.ls_typy_zadan.id);
    
    // projekt
    $("#ProjectSelect").val(taskData.Task.ls_projekty.id);
    
    // nazwa
    $("#NameInput").val(taskData.Task.nazwa);
    
    // opis
    $("#DescribtionTextArea").val(taskData.Task.opis);
    
    // osoby odpowiedzialne
    for(i in taskData.Workers){ 
        var worker = taskData.Workers[i];
        $("input[name=programmer][value=" + worker.id + "]").attr("checked", true);
    }
    
    // status
    $("#StatusSelect").val(taskData.Task.ls_statusy.id);
    
    // poziom waznosci
    $("#UrgencySelect").val(taskData.Task.ls_poziomy_waznosci.id);
    
    var submitButton = $("<input>").attr("type", "button")
                                .attr("id", "SubmitDataButton")
                                .attr("value", "Zmień")
                                .click(OnSubmitData);
                                
    submitButton.appendTo($("#SubmitChangesContainer"));
}

function SetAvailableProjects(projects){

    projects = StripWebServiceResponse(projects);
    var selectProjectTag = document.getElementById("ProjectSelect");
    
    for(i in projects){
        $("<option>").val(projects[i].id).text(projects[i].opis).appendTo(selectProjectTag);
    }

    gotProjects = true;
    RequestTaskData(); 
}

function SetAvailableTaskTypes(taskTypes){

    taskTypes = StripWebServiceResponse(taskTypes);
    var selectTypeTag = document.getElementById("TaskTypeSelect");
    
    for(i in taskTypes){
        $("<option>").val(taskTypes[i].id).text(taskTypes[i].typ_zadania).appendTo(selectTypeTag);
    }

    gotTaskTypes = true;
    RequestTaskData();
}

function SetAvailablePeople(people){

    people = StripWebServiceResponse(people);
    var peopleTag = document.getElementById("People");
    
    for(i in people){
        //$('<input type="checkbox" />').val(people[i].id).text(people[i].imie).html(people[i].imie).appendTo(peopleTag);
        $('<input>').attr('value', people[i].id)
                    .attr('name', "programmer")
                    .attr('type', "checkbox")
                    .appendTo(peopleTag);
        $('<span>').text(people[i].imie).appendTo(peopleTag);
        $('<br>').text("asd").appendTo(peopleTag);
    }
    
    gotPeople = true;
    RequestTaskData();
}

function SetAvailableUrgencyLevels(urgencyLevels){

    urgencyLevels = StripWebServiceResponse(urgencyLevels);
    var selectUrgencyTag = document.getElementById("UrgencySelect");
    
    for(i in urgencyLevels){
        $("<option>").val(urgencyLevels[i].id).text(urgencyLevels[i].waznosc).appendTo(selectUrgencyTag);
    }

    gotUrgencyLevels = true;
    RequestTaskData();
}

function SetAvailableStates(states){

    states = StripWebServiceResponse(states);
    var statusSelect = document.getElementById("StatusSelect");
    
    for(i in states){
        $("<option>").val(states[i].id).text(states[i].status).appendTo(statusSelect);
    }
    
    gotStates = true;
    RequestTaskData();
}